package erfgame.core.world.terrain.color;


import erfgame.core.util.ColorUtils;
import erfgame.core.world.World;
import erfgame.core.world.terrain.TerrainColorSource;
import erfgame.core.world.terrain.TerrainColorSourceFactory;

public class ShadowingTerrainColorSourceFactoryProxy implements
		TerrainColorSourceFactory {
	
	private int shadowColor;
	private int shadowWeight;
	private int colorWeight;
	private TerrainColorSourceFactory proxied;
	
	public ShadowingTerrainColorSourceFactoryProxy(
			TerrainColorSourceFactory proxied,
			int shadowColor, 
			int shadowWeight, 
			int colorWeight 
	) {
		this.proxied = proxied;
		this.shadowColor = shadowColor;
		this.shadowWeight = shadowWeight;
		this.colorWeight = colorWeight;
	}

	public TerrainColorSource createColorSource(World world) {
		final int maxShadowDepth = world.getMaxShadowDepth();
		final int maxColorWeight = maxShadowDepth * colorWeight;
		final TerrainColorSource proxied = this.proxied.createColorSource(world);
		return new TerrainColorSource() {

			public int getColor(
					int x, 
					int y, 
					int z, 
					int mult, 
					int div,
					int dCeiling) {
				int color = proxied.getColor(x, y, z, mult, div, dCeiling);
				if( dCeiling >= 0 ) {
					color = ColorUtils.averageColors(
							shadowColor, 
							Math.max( 0, maxShadowDepth - dCeiling ) * shadowWeight, 
							color, 
							maxColorWeight
					);
				}
				return color;
			}
			
		};
	}
	

}
